/**
 * Created by trisatria on 2/21/14.
 */
var data_to_img_page = '';
$(document).on('pageshow','#productdetail', function(event) {

    var test =  $.session.get("test1");
    console.log(test);
    $.mobile.loading( "show");
    var resault = getUrlVars();
    var product_id = resault['product_id'];
    $('#confirm_yes').attr('href',product_id);
    $.ajax({
        url: _host+'topic/detail/'+product_id
    }).done(function(data){
            $.cookie('detail_product',JSON.stringify(data));
            var j_data = $.parseJSON(data);
            var j_topic = $.parseJSON(j_data['topic']);
            var j_user = $.parseJSON(j_data['user']);
            data_to_img_page = j_data['img'];
            var j_images = $.parseJSON(j_data['img']);
            //var item = '<div class="item"><img src="'+_host_img+'/'+j_topic['images']+'" ></div>';
            //$("#owl-demo").append(item);
            for(var i=0; i< j_images.length; i = parseInt(i) + 1){
                 item = '<div class="item"><img src="'+_host_img+'/'+j_images[i]['link']+'" ></div>';
                 $("#owl-demo").append(item);
            }

        $("#owl-demo").owlCarousel({

            //navigation : true, // Show next and prev buttons
            slideSpeed : 300,
            paginationSpeed : 400,
            singleItem:true,

            // "singleItem:true" is a shortcut for:
             items : 1,
             itemsDesktop : [1000,1],
             itemsDesktopSmall : [900,1],
             itemsTablet: [600,1],
             itemsMobile : [488,1]

        });


            $('#product_title').text(_topic_type[j_topic['topic_type']]+' :: '+j_topic['title']);
            var content = '<div data-demo-html="true">';

            content += '    <div class="ui-grid-solo">';
            content += '<div class="ui-grid-b">';
            content += '    <div class="ui-block-a"><div class="ui-bar ui-bar-b" style="height:60px"><a href="tel:'+j_user['phone']+'" class="ui-btn ui-btn-inline ui-icon-phone ui-btn-icon-left">Gọi</a></div></div>';
            content += '     <div class="ui-block-b"><div class="ui-bar ui-bar-b" style="height:60px"><a  href="sms:'+j_user['phone']+'" class="ui-btn ui-btn-inline ui-icon-comment ui-btn-icon-left"> SMS </a></div></div>';
            content += '       <div class="ui-block-c"><div class="ui-bar ui-bar-b" style="height:60px"><a href="mailto:'+j_user['email']+'" class="ui-btn ui-btn-inline ui-icon-mail  ui-btn-icon-left"> Email </a></div></div>';
                content += ' </div>';
            content += '    </div>';
            content += '    <div class="ui-grid-solo" style="margin-top: 20px">';
            content += '        <div class="ui-block-a"><b>'+_topic_loc[j_topic['topic_location']] +'::'+_topic_cat[parseInt(j_topic['cat_id']) - 1]['title']+'</b></div>';
            content += '        </div>';
            content += '    </div>';

            content += '    <div class="ui-grid-solo">';
            content += '        <div class="ui-block-a"><p style="text-align: center">'+j_topic['full_description']+'</p></div>';
            content += '        </div>';
            content += '    </div>';
            content += '<div class="ui-grid-solo" style=" margin-bottom: 20px">';
            content += '    <div class="ui-block-a">';
            content +=    '<ul data-role="listview" style="margin-top: 0px">';
            content +='         <li><b>'+_topic_type[j_topic['topic_type']] + '::'+j_topic['title']+'</b></li>';
            if(j_topic['show_price'] == 1){
                content +='         <li><b>Giá: </b><span class="price">'+j_topic['price']+'</span></li>';
            }else{
                content +='         <li><b>Giá: </b>Liên Hệ</li>';
            }
            content +='         <li><b>Người Đăng: </b>'+j_user['name']+'</li>';
            content +='         <li><b><a href="tel:'+j_user['phone']+'">Điện Thoại: </b>'+j_user['phone']+'</a></li>';
            content +='         <li><b>Địa Chỉ: </b>'+j_user['address']+'</li>';
            content +='         <li><b><a href="mailto:'+j_user['email']+'">Email: </b>'+j_user['email']+'</a></li>';
            content +='         <li><b>'+ j_topic['create_date']+' <b> '+ $.timeago(j_topic['create_date'])+'</b></li>';
            content +='</ul></div>';
            content += '</div>';
            content += '</div>';

            $('#content_detail').append(content);
            $('.price').formatCurrency($.formatCurrency.regions['vi-VN']);
            var control = ' <ul class="ui-grid-a">';
            control += '<li class="ui-block-a"><a href="editproduct.html?product_id='+j_topic['id']+'" class="ui-btn ui-icon-edit ui-btn-icon-left ui-shadow ui-corner-all">Sửa</a></li>';
            control += '    <li class="ui-block-b"><a data-icon="delete" id="btn_delete" class="ui-shadow-icon ui-btn ui-shadow ui-corner-all ui-icon-delete ui-btn-icon-left" href="">Xoá</a></li>';
            control += '    </ul>';

            if(user_id == j_user['id']){
                $('#product_control').html(control);
            }else{
            }
            $.mobile.loading( "hide");
        });

});
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
$(document).on('click','#btn_delete', function(){
    $('#confirm_delete').popup('open');
});
$(document).on('click','#confirm_yes', function(){
    var p_id = $(this).attr('href');
    $.ajax({
        url: _host+'topic/delete',
        type: 'post',
        data:{product_id: p_id }
    }).done(function(data){
            if(data!= '-1'){
                $('#show_option').popup('close');
                window.history.back();
            }
        });
    return false;
});

//tap
$(document).on("click",".owl-item img",function(){

    var resault = getUrlVars();
    var product_id = resault['product_id'];
    $.mobile.changePage('productimgsinglepage.html?product_id='+product_id, {  role: "page", reverse: true } );
});
//end tap
